//*****************************************************************************
//**************** (C) 2008 by Eleven Engineering Incorporated ****************
//*****************************************************************************
//**
//**		Tabs:	This file looks best with tab stops set every 6 spaces.
//**
//*****************************************************************************
//*****************************************************************************
//**
//** File:		Thread6_Code.asm
//** Project:	Simple Sample Project for WHAM4
//** Revised:	
//**
//** Description:	Code for Thread6
//**             
//**              
//**              
//**
//*****************************************************************************
//*****************************************************************************
Enter_Thread6:

//-----------------------------------------------
// Setup the stack pointer for thread6
		mov   r7,T6_SP		

	mov	r0,37		
	st	r0,RFhopSequence_pntr 				//store channel number pointed to be used
				
#ifdef	_master	

//enter here with CC2400 in state IDLE[1]
		mov	r0,12287 // 12287 
		outp	r0,BBU0brg					//setup baud rate
		mov	r0,0x217B
		outp	r0,BBU0start
		
		inp	r0,GPHin
		bic	r0,r0,_BBUdirection			//BBUtx pin is input
		outp	r0,GPHout
				
		mov	r0,0b0001000000100011
		outp	r0,BBU0cfg0	
		
		bra	Tx_enter



//this is the start of the transmit cycle

RFloopStart_TX:

		
		jsr	r7,putPacket		

		//LED TEST
		inp	r0,GPCin
		bic	r0,r0,_LED1
		outp	r0,GPCout

		inp	r0,GPHin
		bic	r0,r0,_BBUdirection			//BBUtx pin is input
		outp	r0,GPHout
			
		inp	r0,GPEin
		bic	r0,r0,_CC2400_TX
		outp	r0,GPEout	
		bic	r0,r0,_CC2400_RX	
		outp	r0,GPEout					//put CC2400 in state IDLE[1]
	
   Tx_enter:	
		ld	r0,RFhopSequence_pntr			//RF hop Sequence pointer.

	RFloop_TX20:		
		ld	r2,r0,RFhopSequenceTable_38		//load the channel number(range 0->37) for this time slot
		and	r2,r2,0x00ff				//mask off any status bits

		ld	r2,r2,RFchannelRegisterTable		//rf channel in r2
		mov	r1,_FSDIV_regAddr
		jsr	r6,put_CC2400_register			//output to CC2400

		inp	r0,GPEin
		bis	r0,r0,_CC2400_TX
		bis	r0,r0,_CC2400_RX
		outp	r0,GPEout					//start frequency synthesizer(FS) on CC2400
		
	RFloop_TX30:
		mov	r1,_MAIN_regAddr
		jsr	r6,get_CC2400_status
		bic	r1,r1,2
		bc	VC,RFloop_TX30

		bra	RFloopStart_TX
	
#else

//enter here with CC2400 in state IDLE[1]
		mov	r0,12287// 12287
		outp	r0,BBU0brg					//setup baud rate
		mov	r0,0x217B
		outp	r0,BBU0start	

		inp	r0,GPHin
		bic	r0,r0,_BBUdirection			//BBUtx pin is input
		outp	r0,GPHout

		mov	r0,300<<8
		outp	r0,BBU0cfg1	
				
		mov	r0,0b0001000001100011
		outp	r0,BBU0cfg0	
		

Rx_Start:	

		inp	r0,GPEin
		bis	r0,r0,_CC2400_TX
		outp	r0,GPEout					//switch to state FS_On[9]

		inp	r0,GPEin
		bic	r0,r0,_CC2400_TX
		outp	r0,GPEout					
		bic	r0,r0,_CC2400_RX	
		outp	r0,GPEout					//put CC2400 in state IDLE[1]
			
	enter:
		ld	r0,RFhopSequence_pntr

		ld	r2,r0,RFhopSequenceTable_38		//load the channel number(range 0->37) for this time slot
		and	r2,r2,0x00ff				//mask off any status bits

		ld	r2,r2,RFchannelRegisterTable
		
		mov	r1,_FSDIV_regAddr
		jsr	r6,put_CC2400_register			

		inp	r0,GPEin
		bis	r0,r0,_CC2400_TX
		bis	r0,r0,_CC2400_RX
		outp	r0,GPEout					//start frequency synthesizer(FS) on CC2400
		
		
	RFloop_TX30:
		mov	r1,_MAIN_regAddr
		jsr	r6,get_CC2400_status
		bic	r1,r1,2
		bc	VC,RFloop_TX30

		inp	r0,GPEin
		bic	r0,r0,_CC2400_TX
		outp	r0,GPEout					//switch to RX mode
				
RFloopStart_RX:
	
	Generator_CheckFlag:
		ld	r3,CounterFlag
		bc	NS,Generator_CheckFlag		//wait for Thread1 to read updated counter
								//check if clear
		jsr	r7,get_Packet
		
		ld	r3,CounterFlag
		bis	r3,r3,15
		st	r3,CounterFlag		// updated count value received from Tx set flag bit

//LED TEST
		inp	r0,GPCin			//turn on LED after packet is received from Tx
		bic	r0,r0,_LED1
		outp	r0,GPCout

		bra	RFloopStart_RX
#endif				


get_Packet:

	getPacket10:
		mov	r0,0b0001000001100011
		outp	r0,BBU0cfg0					//enable timing recovery
		
getPacket_hunt:

		inp	r0,BBU0rx4
		bc	NC,getPacket_hunt				//loop until we start seeing 0x5555 preamble

getPacket_start:

		inp	r0,BBU0rx4
		bc	NS,getPacket_start			//loop until we have seen 0x217B start word
		
//we have now seen start	
		mov	r0,0b0001000001100111
		outp	r0,BBU0cfg0					//lock BBU0 timing
	
		mov	r5,0x1911					//initialize 16-bit checksum register

		inp	r0,BBU0rx4		
		inp	r0,BBU0rx					//get 16-chips
		
		rol	r5,r5,7
		add	r5,r5,r0					//checksum 16- bit identifier
		sub	r1,r0,0x1234
		bc	NE,getPacket10			
	
	getPacket_data:	

		inp	r0,BBU0rx4					
		inp	r0,BBU0rx					//get 16-chips

		st	r0,Result					//save value received to Result variable
	
		jsr	r7,r7
		
			
putPacket:


//LED TEST
		inp	r0,GPCin
		bic	r0,r0,_LED1
		outp	r0,GPCout

	put_packet_wait:
	
		inp	r0,BBU0time
		sub	r0,r0,r3
		bc	LT0,put_packet_wait
		
		inp	r0,GPEin
		bic	r0,r0,_CC2400_RX
		outp	r0,GPEout				//switch to TX
		
		inp	r0,GPHin
		bis	r0,r0,_BBUdirection		//BBUtx pin is output
		outp	r0,GPHout

		mov	r2,0x5555				//Send Preamble
		outp	r2,BBU0tx				//Output to BBU
		outp	r2,BBU0tx
		outp	r2,BBU0tx
		outp	r2,BBU0tx
//		outp	r2,BBU0tx
//		outp	r2,BBU0tx				
	

		mov	r2,0x217B				//send start of frame character
		outp	r2,BBU0tx				//output to BBU
		
		mov	r5,0x1911				//initialize 16-bit checksum register	
	
		mov	r1,0x1234				//16-bit packet identifier
		rol	r5,r5,7
		add	r5,r5,r1				//checksum

		outp	r1,BBU0tx				//output to BBU

	put_packetData:	
	
		ld	r1,Counter				//load previous counter value
		add	r1,r1,1				//increment
		st	r1,Counter				//store
		
	put_packetData10:
		outp	r1,BBU0tx				//output counter to BBU


//insert a bit of delay here to slow down the counter.  
								
Display_delay:
		mov	r2,1000			//adjustable loop counter
	Display_delay10:	
		inp	r1,SCUtime
		//add	r1,r1,16384			//adjustable time delay(must be < 32767)
		add	r1,r1,32767
	Display_delay20:
		inp	r0,SCUtime
		sub	r0,r0,r1
		bc	LT0,Display_delay20	//wait for time to elapse		
		sub	r2,r2,1			//decrement loop counter
		bc	ZC,Display_delay10
		
		bra	put_packet_wait
	
//		jsr	r7,r7